<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\api\model\v1;
use think\Model;
use think\facade\Db;

class Ad extends Model{
    /**
     * 广告友链
     * num 显示数量
     * lang 语言
     * att_type 属性，link友情链接，banner幻灯片
     * position 位置
     * field 检索字段
     * order 排序
    */
    static function index($num = 5, $lang = 'cn', $att_type = 'banner', $position = 'index', $signid = 1, $field = 'id,title,description,thumb,linkurl', $order = 'sequence desc', $client = ''){
        $where[] = ['lang','=',$lang]; $where[] = ['signid','=',$signid]; $where[] = ['att_type','=',$att_type]; $where[] = ['position','=',$position];
        $ad = Db::name('ad')->where($where);
        if(!empty($client)){
            $ad = $ad->where('find_in_set(\''.$client.'\',client)');
        }
        /*城市分站*/
        $cityid = Db::name('area')->where('bind_domain',request()->host(true))->value('id');
        if(!empty($cityid)) $ad = $ad->whereRaw("cityid = 0 or find_in_set($cityid,cityid)");
        // 参数
        $pageParam = array('lang'=>$lang);
        $pageParam += array('signid'=>$signid);
        $pageParam += array('att_type'=>$att_type);
        $pageParam += array('position'=>$position);
        $pageParam += array('client'=>$client);
        
        $rs = $ad->field($field)->order($order)->paginate([
                'list_rows'=> $num,
                'query'=> $pageParam,
            ]);

        return $rs;
    }
}